/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package foalvleditor.gui;

import foalvleditor.core.ImageSwitcher;
import foalvleditor.core.impl.ClipboardModel;
import foalvleditor.core.impl.LevelModel;
import foalvleditor.gameobjects.AbstractGameObject;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

/**
 *
 * @author Mikael
 */
public class JObjectPopupMenu extends JPopupMenu {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final AbstractGameObject obj;
    private static final LevelModel levelModel=LevelModel.getInstance();
    private static final ClipboardModel clipboardModel=ClipboardModel.getInstance();
    private static ImageSwitcher imageSwitcher=ImageSwitcher.getInstance();
    private final int xMenuPos;

    private final int yMenuPos;

    public JObjectPopupMenu(AbstractGameObject goa, int newXMenuPos, int newYMenuPos){
        super();
        this.obj=goa;
        this.xMenuPos=newXMenuPos;
        this.yMenuPos=newYMenuPos;
        this.add("This object: "+goa.getName()+" [layer "+goa.getLayer()+"]");
        this.addSeparator();
        JMenuItem event=new JMenuItem(new PopupAction("event"));
        JMenuItem image=new JMenuItem(new PopupAction("image"));
        JMenuItem moveup=new JMenuItem(new PopupAction("moveup"));
        JMenuItem movedown=new JMenuItem (new PopupAction("movedown"));
        JMenuItem copy=new JMenuItem (new PopupAction("copy"));
        JMenuItem delete=new JMenuItem(new PopupAction("delete"));
        event.setText("Add event...");
        image.setText("Change image...");
        moveup.setText("Move up");
        movedown.setText("Move down");
        copy.setText("Copy");
        delete.setText("Delete");
        this.add(event);
        this.add(image);
        this.add(moveup);
        this.add(movedown);
        this.add(copy);
        this.addSeparator();
        this.add(delete);

    }

private class PopupAction extends AbstractAction{

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final String action;
    
    public PopupAction (String evt){
        super();
        this.action=evt;
        putValue(Action.NAME, action);
    }

        public void actionPerformed(ActionEvent e) {
            if (action.contains("delete")) levelModel.removeObject(obj);
            if (action.contains("moveup") && obj.getLayer()>0) {
                obj.setLayer(obj.getLayer()-1);
                levelModel.getView().repaintPanel(obj);
                levelModel.getView().repaintAll();
            }
            if (action.contains("movedown") && obj.getLayer()<3) {
                obj.setLayer(obj.getLayer()+1);
                levelModel.getView().repaintPanel(obj);
                levelModel.getView().repaintAll();
            }
            if (action.contains("copy"))
                clipboardModel.copyToClipboard(obj);
         
            if (action.contains("image")){
                imageSwitcher.initSwitchImageDialog(obj, xMenuPos, yMenuPos);
                
            }
            if (action.contains("event")){
                {
                    JNewEventFrame nef=new JNewEventFrame(obj);
                    nef.setLocation(xMenuPos, yMenuPos);
                    nef.setVisible(true);
                }
            }

            }
        }
}

    